import xlsx from "xlsx";
import fileSaver from "file-saver";
/**
 * 导出文件到 Excel 工具类
 *  @需要插件：npm install xlsx --save，npm install file-saver --save
 */
class ExportExcel {
    /**
     * element ui 表格 导出excel
     * @param htmtElement：表格html对象
     * @param fileName：导出的文件名
     */
    public elementUITableToExcel(htmtElement: any, fileName: string, _callback: any = null) {
        const el: any = htmtElement.$el;
        let table = el.querySelector(".el-table__fixed");
        if (!table) {
            table = el;
        }
        const wb = xlsx.utils.table_to_book(table, { raw: true });
        const wbout = xlsx.write(wb, {
            bookType: "xlsx",
            bookSST: true,
            type: "array",
        });
        fileSaver.saveAs(
            new Blob([wbout], { type: "application/octet-stream" }),
          fileName
        );
        if (_callback) {
            _callback();
        }
    }
}
const exportExcel: ExportExcel = new ExportExcel();
export default exportExcel;
